<?php
namespace haibao\cms\business;

use haibao\cms\model\filter\FeaturePublish as FeaturePublishFilter;
use haibao\cms\model\data\FeaturePublish as FeaturePublishModel;
use haibao\cms\data\FeaturePublish as FeaturePublishData;
use haibao\frame\data\query\Condition;
use haibao\classlibrary\cms\common\File as FileUtility;
use haibao\classlibrary\cms\common\Feature as FeatureUtility;
use haibao\cms\data\ZtSpecial as ZtSpecialData;
use haibao\cms\model\filter\ZtSpecial as ZtSpecialFilter;
use haibao\cms\model\filter\FeatureParam as FeatureParamFilter;
use haibao\cms\model\data\FeatureIni as FeatureIniModel;
use haibao\app\business\BusinessException;
use haibao\cms\model\filter\FeatureDefine as FeatureDefineFilter;
use haibao\cms\model\filter\FeatureDefine;

class FeaturePublish extends \haibao\cms\business\Base{
	const FEATURE_CONTENT_TYPE_UPLOAD = 1;
	const FEATURE_CONTENT_TYPE_OTHER_FEATURE = 2;
	
	public static function moveDynamic($offset,$defineId,$direct){
		if (!$defineId){
			throw new BusinessException('动态数据定义id不能为空');
		}
		
		$data = new FeaturePublishData();
		$data->moveDynamic($offset,$defineId,$direct);
	}
	
	public static function addFeatureGrade($gradeModel){
		if (!$gradeModel->FeatureId){
			throw new BusinessException('专题id不能为空');
		}
		
		if (!self::checkAddGradePermission($gradeModel->FeatureId)){
			throw new BusinessException('已经提交过评价，无法再次评价！');
		}
		
		$gradeModel->UserId = self::getCurrentUser()->Id;
		$gradeModel->CreateTime = new \DateTime();
		
		$data = new FeaturePublishData();
		
		$data->add($gradeModel);
		
		self::sendGradeEmail($gradeModel);
	}
	
	public static function sendGradeEmail($gradeModel){
		$feature = self::getFeatureById($gradeModel->FeatureId);
		$user = \haibao\cms\business\User::getUserById($gradeModel->UserId);
		$title = '专题评分：'.$feature->FeatureDefine->FeatureName;
		if ($feature->IsH5){
			$title .= '(Wap站)';
		} else {
			$title .= '(Pc站)';
		}
	
		$content = '设计评分：'.$gradeModel->DesignerGrade."<br/>";
		$content .= '设计评价：'.$gradeModel->DesignerContent."<br/>";
		$content .= '前端评分：'.$gradeModel->HtmlGrade."<br/>";
		$content .= '前端评价：'.$gradeModel->HtmlContent."<br/>";
		$content .= '评论时间：'.date('Y-m-d H:i:s')."<br/>";
		$content .= '评论人：'.$user->UserName.'('.$user->RealName.')';
	
		$to = 'front@haibao.com';
		$mailModel = new \haibao\cms\model\Mail(
				$to,
				$title,
				$content
		);
		\haibao\classlibrary\cms\common\MailHandler::setConfig(
				\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_IP),
				\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_PORT),
				\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_PATH)
		);
		\haibao\classlibrary\cms\common\MailHandler::sendMail($mailModel);
	}
	
	public static function sendTestGroupEmail($featureId){
		$feature = self::getFeatureById($featureId);
		$title = '有新专题添加到专题汇总';
		
		$user = self::getCurrentUser();
		$content = '专题名称：'.$feature->FeatureDefine->FeatureName;
		$content .= '<br />专题地址：'.$feature->Url;
		$content .= '<br />操作时间 ：'.date('Y-m-d H:i:s');
		$content .= '<br />操作人员：'.$user->UserName.'('.$user->RealName.')';
		
		$to = array('guoyiyue@haibao.com','yuanhui@haibao.com');
		$mailModel = new \haibao\cms\model\Mail(
				$to,
				$title,
				$content
		);
		\haibao\classlibrary\cms\common\MailHandler::setConfig(
				\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_IP),
				\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_PORT),
				\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_PATH)
		);
		\haibao\classlibrary\cms\common\MailHandler::sendMail($mailModel);
	}
	
	public static function checkAddGradePermission($featureId){
		if (!$featureId){
			throw new BusinessException('专题id不能为空');
		}
		
		$hasPermission = false;
		$filter = new \haibao\cms\model\filter\FeatureGrade();
		$filter->where($filter::CONDITION_FIELD_NAME_FEATURE_ID,Condition::CONDITION_EQUAL,$featureId);
		$filter->where($filter::CONDITION_FIELD_NAME_USER_ID,Condition::CONDITION_EQUAL,self::getCurrentUser()->Id);
		if (!self::searchFeature($filter)){
			$hasPermission = true;
		}
		
		return $hasPermission;
	}
	
	public static function getGrade($featureIds){
		$data = new FeaturePublishData();

		return $data->getGrade($featureIds);
	}
	
	public static function searchIdsByKeywordFilter($filter){
		$data = new FeaturePublishData();
		
		return $data->searchIdsByKeywordFilter($filter);
	}
	
	public static function getParamValue($featureDefineId,$paramType,$contentType,$dataName){
		$str = '';
		$filter = new \haibao\cms\model\filter\FeatureParam();
		$filter->select(array(
				$filter::CONDITION_FIELD_NAME_VALUE
		));
		$filter->where(
				$filter::CONDITION_FIELD_NAME_FEATURE_ID,
				Condition::CONDITION_EQUAL,
				$featureDefineId		
		);
		$filter->where(
				$filter::CONDITION_FIELD_NAME_PARAM_TYPE,
				Condition::CONDITION_EQUAL,
				$paramType
		);
		$filter->where(
				$filter::CONDITION_FIELD_NAME_CONTENT_TYPE,
				Condition::CONDITION_EQUAL,
				$contentType
		);
		$filter->where(
				$filter::CONDITION_FIELD_NAME_CODE,
				Condition::CONDITION_EQUAL,
				$dataName
		);
		
		$data = new FeaturePublishData();
		$result = $data->getOne($filter);
		if ($result){
			$str = $result->Value;
		}
		
		return $str;
	}
	
	/**
	 * 通过专题id获取动态数据列表
	 */
	public static function getAllDynamicListByFeatureId($featureDefineId){
		if (!$featureDefineId){
			throw new BusinessException('专题Id不能为空');
		}
		
		$data = new FeaturePublishData();
		
		return $data->getAllDynamicListByFeatureId($featureDefineId);
	}
	
	public static function setSvnAddress($featureId,$svnAddress){
		if (!$featureId){
			throw new BusinessException('专题Id不能为空');
		}
		if (!$svnAddress){
			throw new BusinessException('svn地址不能为空');
		}
		
		$svnAddress = ltrim(str_replace('\\','/',$svnAddress),'/');
		
		$filter = new FeaturePublishFilter();
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_SVN_ADDRESS,Condition::CONDITION_EQUAL,$svnAddress);
		if (self::searchFeature($filter)){
			throw new BusinessException('svn地址已被其他专题使用');
		}
		
		$data = new FeaturePublishData();
		$data->updateFeature($featureId, array(
				\haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_SVN_ADDRESS=>$svnAddress
		));
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, array('Id'=>$featureId,'SvnAddress'=>$svnAddress));
	}
	
	public static function addShareCount($featureId,$type){
		$data = new FeaturePublishData();
		
		return $data->addShareCount($featureId,$type);
	}
	
	public static function addPv($featureId){
		$data = new FeaturePublishData();
		
		return $data->addPv($featureId);
	}

	public static function getFeaturePvCount($featureId){
		$data = new FeaturePublishData();
		return $data->getFeaturePvCount($featureId);
	}

	public static function checkCopying($featureId){
		$data = new FeaturePublishData();
		
		return $data->checkCopying($featureId);
	}
	
	public static function getImgList($featureId,$page,$pageSize,$orderType){
		$data = new FeaturePublishData();
		
		return $data->getImgList($featureId,$page,$pageSize,$orderType);
	}
	
	public static function setImgBorder($id,$borderImg){
		$data = new FeaturePublishData();
		
		return $data->setImgBorder($id,$borderImg);
	}
	
	public static function getImgById($id){
		$data = new FeaturePublishData();
		
		return $data->getImgById($id);
	}
	
	public static function addImg($content,$featureId){
		$data = new FeaturePublishData();
		
		return $data->addImg($content,$featureId);
	}
	
	public static function addFeatureRecord($itemModels,$featureId,$defineId){
		if (!$featureId){
			throw new BusinessException('专题id不能为空');
		}
		$data = new FeaturePublishData();
		
		return $data->addFeatureRecord($itemModels,$featureId,$defineId);
	}
	
	public static function updateFeatureIni($featureId,$id,$arrSet){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		if (!$featureId){
			throw new BusinessException('专题id不能为空');
		}
		
		$data = new FeaturePublishData();
		$data->updateFeatureIni($featureId,$id, $arrSet);
		
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT,array_merge($arrSet,array('featureId'=>$featureId)));
	}
	
	public static function updateFeatureParamValue($id,$arrSet,$featureId){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		
		$data = new FeaturePublishData();
		$data->updateFeatureParamById($id, $arrSet,$featureId);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT,array_merge($arrSet,array('paramId'=>$id)));
	}
	
	public static function getAllDynamicItems($filter){
		$data = new FeaturePublishData();
		
		return $data->getAll($filter);
	}
	
	public static function delDynamicDefine($defineId){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		if (!$defineId){
			throw new BusinessException('动态数据定义id不能为空');
		}
		
		$data = new FeaturePublishData();
		$data->delDynamicDefine($defineId);
	
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, array('delDynamicDefine'=>$defineId));
	}
	
	private static function checkFeatureDefineCode($defineModel,$edit = false){
		//判断别名是否重复
		$defineFilterModel = new \haibao\cms\model\filter\FeatureGroupDefine();
		$defineFilterModel->where(
			\haibao\cms\model\filter\FeatureGroupDefine::CONDITION_FIELD_NAME_GROUP_NAME,
			Condition::CONDITION_EQUAL,
			$defineModel->GroupName
		);
		$defineFilterModel->where(
			\haibao\cms\model\filter\FeatureGroupDefine::CONDITION_FIELD_NAME_FEATURE_ID,
			Condition::CONDITION_EQUAL,
			$defineModel->FeatureId
		);
		if ($edit){
			$defineFilterModel->where(
				\haibao\cms\model\filter\FeatureGroupDefine::CONDITION_FIELD_NAME_ID,
				Condition::CONDITION_NOTEQUAL,
				$defineModel->Id		
			);
		}
		
		$data = new FeaturePublishData();
		if ($result = $data->getAll($defineFilterModel)){
			throw new BusinessException('动态数据别名重复');
		}
		
		//判断code是否重复
		$codes = array();
		foreach ($defineModel->DefineLists as $list){
			array_push($codes,$list->Code);
		}
		$listFilter = new \haibao\cms\model\filter\FeatureDynamicDefineList();
		$defineFilterModel = new \haibao\cms\model\filter\FeatureGroupDefine();
		$listFilter->where(
				\haibao\cms\model\filter\FeatureDynamicDefineList::CONDITION_FIELD_NAME_CODE,
				Condition::CONDITION_IN,
				$codes
		);
		$defineFilterModel->where(
				\haibao\cms\model\filter\FeatureGroupDefine::CONDITION_FIELD_NAME_FEATURE_ID,
				Condition::CONDITION_EQUAL,
				$defineModel->FeatureId
		);
		$listFilter->innerJoin(
				$defineFilterModel,
				null,
				\haibao\cms\model\filter\FeatureDynamicDefineList::CONDITION_FIELD_NAME_DEFINE_ID,
				\haibao\cms\model\filter\FeatureGroupDefine::CONDITION_FIELD_NAME_ID,
				\haibao\cms\model\filter\FeatureDynamicDefineList::CONDITION_FIELD_NAME_DEFINE
		);
		
		if ($edit){
			$listFilter->where(
					\haibao\cms\model\filter\FeatureDynamicDefineList::CONDITION_FIELD_NAME_DEFINE_ID,
					Condition::CONDITION_NOTEQUAL,
					$defineModel->Id
			);
		}
		
		if ($result = $data->getAll($listFilter)){
			throw new BusinessException('动态数据名称重复');
		}
	}
	
	public static function addDynamicDefine($model){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		self::checkFeatureDefineCode($model);
		
		$data = new FeaturePublishData();
		$data->addDynamicDefine($model);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, $model);
	}
	
	public static function updateDynamicDefine($model){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		if (!$model){
			throw new BusinessException('动态数据实体不能为空');
		}
		self::checkFeatureDefineCode($model,true);
		
		$data = new FeaturePublishData();
		$data->updateDynamicDefine($model);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, $model);
	}
	
	public static function delParamById($paramId){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		if (!$paramId){
			throw new BusinessException('参数id不能为空');
		}
		
		$data = new FeaturePublishData();
		$data->delParamById($paramId);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, array('delFeatureParam'=>$paramId));
	}
	
	public static function updateFeatureParam($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		if (!$id){
			throw new BusinessException('参数id不能为空');
		}
		
		$filter = new FeatureParamFilter();
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_FEATURE_ID,Condition::CONDITION_EQUAL,$arrSet[FeatureParamFilter::CONDITION_FIELD_NAME_FEATURE_ID]);
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_CODE,Condition::CONDITION_EQUAL,$arrSet[FeatureParamFilter::CONDITION_FIELD_NAME_CODE]);
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_PARAM_TYPE,Condition::CONDITION_EQUAL,$arrSet[FeatureParamFilter::CONDITION_FIELD_NAME_PARAM_TYPE]);
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$id);
		
		if (self::getAllParamList($filter)){
			throw new BusinessException('参数名称已存在');
		}
		
		$data = new FeaturePublishData();
		$data->updateFeatureParam($id,$arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, array(array_merge($arrSet,array('id'=>$id))));
	}
	
	public static function addFeatureParam($model){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		
		$filter = new FeatureParamFilter();
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_FEATURE_ID,Condition::CONDITION_EQUAL,$model->FeatureId);
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_CODE,Condition::CONDITION_EQUAL,$model->Code);
		$filter->where(FeatureParamFilter::CONDITION_FIELD_NAME_PARAM_TYPE,Condition::CONDITION_EQUAL,$model->ParamType);
		if (self::getAllParamList($filter)){
			throw new BusinessException('参数名称已存在');
		}
		
		$model->CreateTime = new \DateTime();
		$model->CreateBy = self::getCurrentUser()->Id;
		$data = new FeaturePublishData();
		$data->add($model);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, $model);
	}
	
	public static function addFeatureIni($model){
		self::checkOperation(self::FUNC_CODE_FEATURE_PARAM_MANAGER);
		if (!$model->FeatureId){
			throw new BusinessException('专题id不能为空');
		}
		
		$data = new FeaturePublishData();
		
		$data->addFeatureIni($model);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PARAM_MANAGER, $model);
	}
	
	public static function getAllIni($filter){
		$data = new FeaturePublishData();
		
		return $data->getAll($filter);
	}
	
	public static function getAllDynamicDefineList($filter){
		$data = new FeaturePublishData();
		
		return $data->getAll($filter);
	}
	
	public static function getAllParamList($filter){
		$data = new FeaturePublishData();
		
		return $data->getAll($filter);
	}
	
	public static function getAllHrefRecordList($filter){
		$data = new FeaturePublishData();
		
		return $data->getAll($filter);
	}
	
	public static function listFeature($featureId,$isList){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		
		if (!$featureId){
			throw new BusinessException('专题Id不能为空');
		}
		
		if ($isList == FeaturePublishModel::IS_LIST_DISABLE){
			$isList = FeaturePublishModel::IS_LIST_ENABLE;
		}else{
			$isList = FeaturePublishModel::IS_LIST_DISABLE;
		}
		
		$data = new FeaturePublishData();
		$data->listFeature($featureId,$isList);
		
		if ($isList == FeaturePublishModel::IS_LIST_ENABLE){
			self::sendTestGroupEmail($featureId);
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, array('修改专题显示状态',array($featureId=>$isList)));
	}
	
	public static function getDynamicList($featureDefineId,$parameters){
		$data = new FeaturePublishData();
		
		return $data->getDynamicList($featureDefineId,$parameters);
	}
	
	public static function delDynamic($itemGroupId){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		$data = new FeaturePublishData();
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, array('删除动态数据',$itemGroupId));
		
		return $data->delDynamic($itemGroupId);
	}
	
	public static function getAllDynamic($filter){
		$data = new FeaturePublishData();
		
		return $data->getAll($filter);
	}
	
	/**
	 * 添加专题动态数据
	 * @param unknown $defineModel
	 * @param unknown $itemModels
	 * @param unknown $itemDataModels
	 */
	public static function addFeatureDynamic($defineId,$itemDatas,$showTime,$dynamicId){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		$data = new FeaturePublishData();
		
		$data->beginTranscation();
		try{
			$data->endTranscation();
			
			//添加item，group对应关系
			$itemGroupModel = new \haibao\cms\model\data\FeatureItemGroupData();
			$itemGroupModel->GroupDefineId = $defineId;
			$itemGroupModel->CreateTime = new \DateTime();
			$itemGroupModel->CreateBy = self::getCurrentUser()->Id;
			$itemGroupModel->ShowTime = $showTime;
			//$itemGroupModel->OrderNum = $order ? $order : 0;
			if (!$showTime){
				$itemGroupModel->ShowTime = $itemGroupModel->CreateTime;
			}
			$data->add($itemGroupModel);
			//添加itemdata
			$itemDataModels = array();
			foreach ($itemDatas as $key=>$value){
				$itemDataModel = new \haibao\cms\model\data\FeatureGroupDefineItemData();
				$itemDataModel->GroupDefineItemId = $value->Id;
				$itemDataModel->ItemGroupDataId = $itemGroupModel->Id;
				$itemDataModel->ItemValue = $value->Value;
				array_push($itemDataModels,$itemDataModel);
			}
			
			$data->addAll($itemDataModels);
			
			/*if ($dynamicId){
				$data->updateAppendDynamic($dynamicId,$order,$itemGroupModel);
			}*/
		}
		catch (\Exception $e){
			$data->rollback();
			throw $e;
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, $itemDataModels);
	}
	
	/**
	 * 更新动态数据
	 */
	public static function updateFeatureDynamic($itemDataModels,$itemGroupId,$showTime){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		
		$data = new FeaturePublishData();
		$data->updateFeatureDynamic($itemDataModels,$itemGroupId,$showTime);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, $itemDataModels);
	}
	
	public static function getFeatureById($id){
		$models = self::getFeatureByIds(array($id));
		
		return array_pop($models);
	}
	
	public static function getFeatureDefineById($id){
		$models = self::getFeatureDefineByIds(array($id));
		
		return array_pop($models);
	}
	
	public static function getFeatureDefineByIds($ids){
		$filter = new FeaturePublishFilter();
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
		$filter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_CREATE_BY,
				FeaturePublishFilter::CONDITION_FIELD_NAME_CREATE_TIME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_TIME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_BY,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_BY,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_TIME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_URL,
				FeaturePublishFilter::CONDITION_FIELD_NAME_ZIP_FILE_NAME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_IS_H5,
				FeaturePublishFilter::CONDITION_FIELD_NAME_IS_LIST,
				FeaturePublishFilter::CONDITION_FIELD_NAME_SVN_ADDRESS,
		));
		$defineFilter = new FeatureDefineFilter();
		$defineFilter->where(FeatureDefine::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$ids);
		$defineFilter->select(array(
				FeatureDefineFilter::CONDITION_FIELD_NAME_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_COLUMN_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				FeatureDefineFilter::CONDITION_FIELD_NAME_FEATURE_NAME,
				FeatureDefineFilter::CONDITION_FIELD_NAME_ISSUE,
				FeatureDefineFilter::CONDITION_FIELD_NAME_SERIAL_ID
		));
		$defineFilter->innerJoin(
				$filter,
				null,
				FeatureDefineFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_FEATURE_PUBLISHES,
				true
		);
		
		return self::searchFeature($defineFilter);
	}
	
	public static function getFeatureByIds($ids){
		$filter = new FeaturePublishFilter();
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$ids);
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
		$filter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_CREATE_BY,
				FeaturePublishFilter::CONDITION_FIELD_NAME_CREATE_TIME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_TIME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_BY,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_BY,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_TIME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_URL,
				FeaturePublishFilter::CONDITION_FIELD_NAME_ZIP_FILE_NAME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_IS_H5,
				FeaturePublishFilter::CONDITION_FIELD_NAME_IS_LIST,
				FeaturePublishFilter::CONDITION_FIELD_NAME_SVN_ADDRESS,
		        FeaturePublishFilter::CONDITION_FIELD_NAME_PV_COUNT,
				FeaturePublishFilter::CONDITION_FIELD_NAME_SHOW_PV_COUNT,
		));
		$defineFilter = new FeatureDefineFilter();
		$defineFilter->select(array(
				FeatureDefineFilter::CONDITION_FIELD_NAME_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_COLUMN_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_DESCRIPTION,
				FeatureDefineFilter::CONDITION_FIELD_NAME_FEATURE_NAME,
				FeatureDefineFilter::CONDITION_FIELD_NAME_ISSUE,
				FeatureDefineFilter::CONDITION_FIELD_NAME_SERIAL_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_KEYWORDS,
				FeatureDefineFilter::CONDITION_FIELD_NAME_HTML_TITLE,
				FeatureDefineFilter::CONDITION_FIELD_NAME_HTML_KEYWORDS,
				FeatureDefineFilter::CONDITION_FIELD_NAME_HTML_DESCRIPTION,
				FeatureDefineFilter::CONDITION_FIELD_NAME_CLASS_ID,
		));
		$filter->innerJoin(
				$defineFilter,
				null,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE,
				false
		);
		
		return self::searchFeature($filter);
	}
	
	public static function getFeaturePublishByIds($featureIds){
	    $data = new FeaturePublishData();
	    return $data->getFeaturePublishByIds($featureIds);
	}
	
	public static function searchFeatureVote($filter){
		$data = new FeaturePublishData();
	
		return $data->getAll($filter);
	}
	
	public static function vote($id,$name){
		if (!$id){
			throw new BusinessException('专题id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		$data = new FeaturePublishData();
		
		return $data->vote($id,$name);
	}
	
	public static function unVote($id,$name){
		if (!$id){
			throw new BusinessException('专题id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		$data = new FeaturePublishData();
		
		return $data->unVote($id,$name);
	}
	
	public static function publish($id,$url,$isH5,$listTime){
		self::checkOperation(self::FUNC_CODE_FEATURE_PUBLISH);
		if (!$id){
			throw new BusinessException('专题id不能为空',self::DATA_EMPTY_FEATURE_ID);	
		}
		if (!$url){
			throw new BusinessException('专题url不能为空');
		}
		$url = FeatureUtility::getFeatureUrl($url, $isH5);
		//self::checkUrl($url,$id);
		//保存记录到原cms
		if ($model = self::getFeatureById($id)){
			$model->Url = $url;
			$model->PublishTime = new \DateTime();
			$model->PublishBy = self::getCurrentUser()->Id;
			$model->Status = FeaturePublishModel::STATUS_PUBLISH;
			$model->UpdateBy = self::getCurrentUser()->Id;
			$model->UpdateTime = new \DateTime();
			$model->IsH5 = $isH5;
			$model->ListTime = $listTime;
						
			$featureData = new FeaturePublishData();
			
			$featureData->beginTranscation();
			try{
				$arrSet = array(
						FeaturePublishFilter::CONDITION_FIELD_NAME_URL=>$model->Url,
						FeaturePublishFilter::CONDITION_FIELD_NAME_LIST_TIME=>$model->ListTime,
						FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_TIME=>$model->PublishTime,
						FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_BY=>$model->PublishBy,
						FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS=>$model->Status,
						FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_BY=>$model->UpdateBy,
						FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_TIME=>$model->UpdateTime,
						FeaturePublishFilter::CONDITION_FIELD_NAME_IS_H5=>$model->IsH5,
						FeaturePublishFilter::CONDITION_FIELD_NAME_LIST_TIME=>$model->ListTime
				);
				$featureData->updateById($id, $arrSet);
				if ($model->FeatureDefine->SerialId){
					$data = new \haibao\cms\data\FeatureSerial();
					$data->editSerial($model->FeatureDefine->SerialId, array(
							\haibao\cms\model\filter\FeatureSerial::CONDITION_FIELD_NAME_LAST_PUBLISH_TIME=>new \DateTime()
					));
				}
				
				if ($oldFeatureId = $featureData->getDataMigration($model, $model->Id)){
					//更新
					$ztSpecialData = new ZtSpecialData();
					$oldFeature = $ztSpecialData->getOneById($oldFeatureId);
					$featureData->updateOldFeatureRecord($model,$oldFeature);
				}
				else{
					//第一次发布
					$oldFeatureId = $featureData->saveOldFeatureRecord($model);
					$featureData->addDataMigrationMap($model, $oldFeatureId);
					$featureData->updateStatisticEndTime($id,date('Y-m-d',strtotime('+1 months')));
				}
			} catch (\Exception $e){
				$featureData->rollback();
				throw $e;
			}
			$featureData->endTranscation();
			
			self::copyFeatureFile($model,$oldFeatureId);
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_PUBLISH, array($id));
	}
	
	public static function copyFeatureFile($model,$oldFeatureId){
		try {
			//编译专题并上传
			//保存压缩包至mongo
			self::updateFeatureZip(null,$model);
			$combinePath = FeatureUtility::combineFeature($model,$oldFeatureId);
			if ($combinePath){
				FeatureUtility::uploadFeature($model->Url, $combinePath,$model->IsH5);
			}
		} catch (\Exception $e){
            $content = '';
            //$user = \haibao\cms\business\User::getUserById($gradeModel->UserId);
            $user = 'TEST';
			$title = '发布专题文件出错：'.$model->Url;
			if ($model->IsH5){
				$title .= '(Wap站)';
			} else {
				$title .= '(Pc站)';
			}
		
			$content .= '发布人：'.$model->UpdateBy."\r\n<br/>";
            $content .= '异常信息：'.$e->getMessage();

			$to = 'tech@haibao.com';
			$mailModel = new \haibao\cms\model\Mail(
					$to,
					$title,
					$content
			);
			\haibao\classlibrary\cms\common\MailHandler::setConfig(
					\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_IP),
					\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_PORT),
					\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MAIL_BUSINESS_SERVER_PATH)
			);
			\haibao\classlibrary\cms\common\MailHandler::sendMail($mailModel);
		}
	}
	
	public static function getVideoCates($filter){
		$data = new FeaturePublishData();
		
		return $data->getVideoCates($filter);
	}
	
	public static function getFeatureImgPath($features){
		$result = array();

		if ($features){
			$ids = array();
			foreach ($features as $feature){
				if ($feature->PictureId && $feature->PictureId){
					$imgPath = FeatureUtility::getFeatureTmpPath().'/'.md5($feature->PictureId).'/'.FileUtility::getMd5FileName($feature->PictureName);
					if (!file_exists($imgPath)){
						array_push($ids,$feature->PictureId);
					}else{
						$viewUrl = '/'.FeatureUtility::FEATURE_BASE_URL.'/'.FeatureUtility::FEATURE_TMP_PATH.'/'.md5($feature->PictureId).'/'.FileUtility::getMd5FileName($feature->PictureName);
						$result[$feature->PictureId] = $viewUrl;
					}
				}	
			}
			
			if ($ids){
				$data = new FeaturePublishData();
				$files = $data->getFeatureImgByIds($ids);
				if ($files){
					foreach ($files as $file){
						$id = strval($file->file['_id']);
						$fileName = $file->file['fileName'];
						$imgPath = FeatureUtility::getFeatureTmpPath().'/'.md5($id).'/'.FileUtility::getMd5FileName($fileName);
						FileUtility::filePutContents($imgPath,$file->getBytes());
						$viewUrl = '/'.FeatureUtility::FEATURE_BASE_URL.'/'.FeatureUtility::FEATURE_TMP_PATH.'/'.md5($id).'/'.FileUtility::getMd5FileName($fileName);
						$result[$id] = $viewUrl;
					}
				}
			}
		}
		
		return $result;
	}
	
	/**
	 * 重新上传Feature压缩包并解压
	 * @param string $tmpName
	 * @param string $fileName
	 * @param string $toPath
	 */
	/* public static function reUploadFeatureZip($file,$toPath){
		$path = self::uploadFile($file);
		if ($path){
			$path = FeatureUtility::getFeatureFilePath().$path;	
			if (!FeatureUtility::checkMainFileNameFromZip($path)){
				throw new BusinessException('缺少主文件');
			}
				
			$zip = new \ZipArchive();
			$zip->open($path);
			if (file_exists($toPath)){
				FileUtility::removeResource($toPath,false);
			}
			$zip->extractTo($toPath);
			$zip->close();
		}
	} */
	
	public static function reuploadfeatureSvnUrl($svnUrl,$trueToPath,$featureName){
		$dir = FeatureUtility::getFeatureTmpPath().'/'.date('YmdHis').mt_rand(100,900).'/'.md5($featureName);
		FileUtility::makeNewDir($dir);
		//if(!SvnUtility::checkout(FeatureUtility::getFullSvnUrl($svnUrl), $dir))
		$svnUrl = ltrim(str_replace('\\','/',$svnUrl),'/');
		if (!FeatureUtility::checkoutSvn($svnUrl,$dir))
		{
			throw new BusinessException('从svn获取专题失败');
		}
		if (!FeatureUtility::checkMainFileNameFromDir($dir)){
			throw new BusinessException('缺少主文件');
		}
		FileUtility::copyResource($dir, $trueToPath);
	}
	
	public static function updateFeatureZip($id,$model = null){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		
		if (!$model){
			$model = self::getFeatureById($id);	
		}
		
		if ($model){
			$fileId = $model->FileId;
			$zipName = md5($model->FeatureDefine->FeatureName.$model->IsH5).'.zip';
			$truePath = FeatureUtility::getFeatureFilePath().'/'.md5($model->FeatureDefine->FeatureName.$model->IsH5);
			$zipPath = FeatureUtility::getFeatureTmpPath().'/'.FileUtility::getMd5FileName($zipName);
			FileUtility::zipDir($truePath, $zipPath);
			/* $content = file_get_contents($zipPath);
			
			$data = new FeaturePublishData();
			$newFileId = $data->storeFeatureZip($content,array('fileName'=>$zipName));
			$data->updateById($model->Id, array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_FILE_ID=>$newFileId,
				FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_BY =>self::getCurrentUser()->Id,
				FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_TIME=>new \DateTime()
			));
			$data->delFeatureZipById($fileId); */
			FeatureUtility::saveFeatureZipFile($model->ZipFileName,$zipPath);
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, array('ZipFileName'=>$model->ZipFileName,'','Id'=>$model->Id));
	}
	
	public static function getFeatureZipById($id){
		$data = new FeaturePublishData();

		return $data->getFeatureZipById($id);
	}
	
	public static function getFeatureZipByFeatureId($featureId){
		$feature = self::getFeatureById($featureId);
		
		return self::getFeatureZipById($feature->FileId);
	}
	
	public static function delFeatureNew($id){
		self::checkOperation(self::FUNC_CODE_FEATURE_DEL);
		if (!$id){
			throw new BusinessException('专题Id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		
		$data = new FeaturePublishData();
		$filter = new FeaturePublishFilter();
		$filter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_FILE_ID
		));
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,Condition::CONDITION_EQUAL,$id);
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_BY,Condition::CONDITION_NOTEQUAL,0);
		$models = self::searchFeature($filter);
		
		if ($models){
			throw new BusinessException('已发布的专题不允许删除');
		}
		
		try{
			$data->beginTranscation();
			$arrSet = array(
					\haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_STATUS=>\haibao\cms\model\data\FeaturePublish::STATUS_DISABLE
			);
			$query = new FeaturePublishFilter();
			$query->where(FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,Condition::CONDITION_EQUAL,$id);
			$data->update($arrSet, $query);
			$data->endTranscation();
		}
		catch (\Exception $e){
			$data->rollback();
			throw $e;
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_DEL, $id);
	}
	
	public static function delFeature($id){
		self::checkOperation(self::FUNC_CODE_FEATURE_DEL);
		if (!$id){
			throw new BusinessException('专题Id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		
		$data = new FeaturePublishData();
		$filter = new FeaturePublishFilter();
		$filter->select(array(
			FeaturePublishFilter::CONDITION_FIELD_NAME_FILE_ID
		));
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$id);
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,FeaturePublishModel::STATUS_PUBLISH);
		$models = self::searchFeature($filter);
		
		if ($models){
			throw new BusinessException('已发布的专题不允许删除');
		}
		
		$model = array_pop($models);
		try{
			$data->beginTranscation();
			$arrSet = array(
				\haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_STATUS=>\haibao\cms\model\data\FeaturePublish::STATUS_DISABLE
			);
			$data->updateById($id, $arrSet);
			/*
			 * 暂时不删附件 
			$data->delFeatureZipById($model->FileId);
			if ($model->PictureId){
				$data->delFeatureImgById($model->PictureId);
			} */
			$data->endTranscation();
		}
		catch (\Exception $e){
			$data->rollback();
			throw $e;	
		}

		self::saveOperateHistory(self::FUNC_CODE_FEATURE_DEL, $id);
	}
	
	public static function updateFeatureNew($model){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		if (!$model->Id){
			throw new BusinessException('专题id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		if (!$model->FeatureName){
			throw new BusinessException('专题名称不能为空');
		}
		
		$defineFilter = new FeatureDefineFilter();
		$defineFilter->where(FeatureDefineFilter::CONDITION_FIELD_NAME_FEATURE_NAME,Condition::CONDITION_EQUAL,$model->FeatureName);
		$defineFilter->select(array(
				FeatureDefineFilter::CONDITION_FIELD_NAME_ID,
		));
		
		$filter = new FeaturePublishFilter();
		$filter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,
		));
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,CONDITION::CONDITION_EQUAL,FeaturePublishModel::STATUS_ENABLE);
		$defineFilter->innerJoin(
				$filter,
				null,
				FeatureDefineFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				FeatureDefineFilter::CONDITION_FIELD_NAME_FEATURE_PUBLISHES,
				true		
		);
		$result = self::searchFeature($defineFilter);
		
		if ($result && $result[0]->Id != $model->Id){
			throw new BusinessException('专题名称已存在',self::DATA_ERROR_FEATURE_NAME_EXIST); 
		}
		$dbDefineModel = self::getFeatureDefineById($model->Id);
		$publishStatusArr = array();
		foreach ($dbDefineModel->FeaturePublishes as $feature){
			if ($feature->Status == FeaturePublishModel::STATUS_PUBLISH){
				$publishStatusArr[$feature->Id] = 1;
			}
		}
		
		if ($model->FeaturePublishes){
			foreach ($model->FeaturePublishes as $feature){
				if ($feature->Url){
					self::checkUrl($feature->Url,$feature->Id);
				}
			}
		}
		
		self::dealContent($model,true);
		self::dealPic($model);
		$currentUser = self::getCurrentUser();
		$timeNow = new \DateTime();
		
		$data = new FeaturePublishData();
		$data->beginTranscation();
		try{
			$arrSet = array();
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_FEATURE_NAME] = $model->FeatureName;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_SERIAL_ID] = $model->SerialId;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_ISSUE] = $model->Issue;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_DESCRIPTION] = $model->Description;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_CLASS_ID] = $model->ClassId;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_COLUMN_ID] = $model->ColumnId;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_KEYWORDS] = $model->Keywords;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_UPDATE_BY] = $currentUser->Id;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_UPDATE_TIME] = $timeNow;
			$arrSet[FeatureDefineFilter::CONDITION_FIELD_NAME_HTML_TITLE] = $model->HtmlTitle;
			
			$polymerInfoDataVal = array();
			$polymerInfoDataVal['Title'] = $model->FeatureName;
			$polymerInfoDataVal['Summary'] = $model->Description;
			
			$data->setModelName('\haibao\cms\model\data\FeatureDefine');
			$data->updateById($model->Id, $arrSet);
			$data->setModelName('\haibao\cms\model\data\FeaturePublish');
			
			if ($model->Events){
				$data->saveFeatureEventRelation($model->Id,$model->Events,true);
			}
			if ($model->Tags){
				$data->saveFeatureTagRelation($model->Id, $model->Tags,true);
			}
			
			foreach ($model->FeaturePublishes as $feature){
				if ($feature->Id){
					$arrSetFeature = array(
							FeaturePublishFilter::CONDITION_FIELD_NAME_URL=>$feature->Url,
							FeaturePublishFilter::CONDITION_FIELD_NAME_LIST_TIME=>$feature->ListTime,
							FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_BY=>$currentUser->Id,
							FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_TIME=>$timeNow
					);
					if ($feature->PictureId){
						$arrSetFeature[FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID] = $feature->PictureId;
						
						//操作聚合记录
						$polymerInfoData = new \haibao\cms\data\PolymerInfo();
						$arrPolySet['Status'] = \haibao\cms\model\data\PolymerInfo::STATUS_EDIT;
						$objectType = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_FEATURE;
						if($feature->IsH5){
							$objectType = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_H5;
						}
						$polymerInfoData->updatePolymarInfo($model->Id,$objectType,$arrPolySet);
					}
					
					$data->updateById($feature->Id, $arrSetFeature);
				
					if (!empty($publishStatusArr[$feature->Id])){
						$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_TITLE] = $model->FeatureName;
						$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_DESCRIPTION] = $model->Description;
						if ($feature->Url){
							$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_URL] = $feature->Url;
						}
						$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_TAG] = $model->ClassId;
						$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_ZT_VIDEO_CATE] = $model->ColumnId;
						$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_KEYWORDS] = $model->Keywords;
						$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_LIST_DATE] = $feature->ListTime;
						
						if ($feature->PictureName){
							$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_PIC] = $feature->PictureName;
						}
						
						$ztSpecialId = $data->getDataMigration($feature, $feature->Id);
						$ztSpecialData = new ZtSpecialData();
						$ztSpecialData->updateById($ztSpecialId, $oldFeatureSet);
						
						if($model->Tags){
							$tagIdList = \haibao\cms\business\Tag::loopGetParentTag($model->Tags);
							
							$tagIdsStr = '';
							$tagIdsStrArr = array();
							if($tagIdList){
								$tagIdsStr = implode('|', array_values($tagIdList));
								$tagIdsStrArr = array_values($tagIdList);
							}
							
							$polymerInfoDataVal['PublishTime'] = $feature->ListTime;
							$polymerInfoDataVal['ObjectType'] = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_FEATURE;
							if($feature->IsH5){
								$polymerInfoDataVal['ObjectType'] = \haibao\cms\model\data\PolymerInfo::OBJECT_TYPE_H5;
							}
							
							$polymerInfoData = new \haibao\cms\data\PolymerInfo();
							$polymerInfoData->processPolymerInfoFeature($model->Id,$polymerInfoDataVal,$tagIdsStr,$tagIdsStrArr);
						}
					}
				}else{
					$feature->FeatureDefineId = $model->Id;
					$feature->CreateBy = $feature->UpdateBy = $currentUser->Id;
					$feature->CreateTime = $feature->UpdateTime = $timeNow;
					$feature->Status = FeaturePublishModel::STATUS_ENABLE;
					$data->add($feature);
					
					if ($feature->ContentType == self::FEATURE_CONTENT_TYPE_OTHER_FEATURE){
						$otherFeature = self::getFeatureById($feature->OtherFeatureId);
						//同步参数
						$data->copyFeatureParam($model->Id,$otherFeature->FeatureDefineId);
						//同步ini
						$data->copyFeatureIni($feature->Id,$feature->OtherFeatureId);
						//同步动态数据
						$data->copyFeatureDynamic($model->Id,$otherFeature->FeatureDefineId);
					}else{
						$iniModel = new FeatureIniModel();
						$iniModel->WeixinShare = FeatureIniModel::STATUS_DISABLE;
						$iniModel->Vote = FeatureIniModel::STATUS_DISABLE;
						$iniModel->PcShare = FeatureIniModel::STATUS_DISABLE;
						$iniModel->HaibaoHeader = FeatureIniModel::HEADER_DISABLE;
						$iniModel->HaibaoBottom = FeatureIniModel::STATUS_DISABLE;
						$iniModel->GoTop = FeatureIniModel::STATUS_DISABLE;
						$iniModel->Dynamic = FeatureIniModel::STATUS_DISABLE;
						$iniModel->Record = FeatureIniModel::STATUS_DISABLE;
						$iniModel->FeatureId = $feature->Id;
						$data->add($iniModel);
					}
				}
			}
			
			//如果选择了所属系列及期号，更新serial表
			if ($model->SerialId && $model->Issue){
				$serialData = new \haibao\cms\data\FeatureSerial();
				$serialData->updateById($model->SerialId, array(
						\haibao\cms\model\filter\FeatureSerial::CONDITION_FIELD_NAME_CURRENT_ISSUE=>$model->Issue
				));
			}
		} catch (\Exception $e){
			$data->rollback();
			throw $e;
		}
		$data->endTranscation();
		
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, $model->toArray());
	}
	
	/**
	 * 修改专题
	 * @param int $id
	 * @param array $arrSet
	 */
	public static function updateFeature($id,$arrSet,$imgPath,$pictureId){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		if (!$id){
			throw new BusinessException('专题id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		if (isset($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_NAME])){
			$featureName = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_NAME];
			
			if (!$featureName){
				throw new BusinessException('专题名称不能为空',self::DATA_EMPTY_FEATURE_NAME);
			}
			
			$filter = new FeaturePublishFilter();
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_NAME,Condition::CONDITION_EQUAL,$featureName);
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$id);
			if (self::searchFeature($filter)){
				throw new BusinessException('专题名称已存在',self::DATA_ERROR_FEATURE_NAME_EXIST);
			}	
		}
		
		if (!empty($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL])){
			self::checkUrl($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL],$id);
		}
		
		$arrSet = array_merge($arrSet,array(
			FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_BY=>self::getCurrentUser()->Id,
			FeaturePublishFilter::CONDITION_FIELD_NAME_UPDATE_TIME=>new \DateTime()
		));
		
		$model = self::getFeatureById($id);
		$data = new FeaturePublishData();
		
		try{
			$data->beginTranscation();
			$trueImgPath = '';
			if ($imgPath){
				$trueImgPath = FeatureUtility::getFeatureFilePath().$imgPath;
				$arr = array(
						'tmp_name'=>$trueImgPath,
						'name'=>$trueImgPath
				);
				$imgInfo = \haibao\cms\business\ImageLibrary::saveUpload($arr, \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_FEATURE,false);
				$arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID] = $imgInfo['id'];
			}
			
			$data->updateFeature($id,$arrSet);
			
			//如果选择了所属系列及期号，更新serial表
			if (
					!empty($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_SERIAL_ID]) 
				&& 	!empty($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_ISSUE])
			){
				$serialData = new \haibao\cms\data\FeatureSerial();
				$serialData->updateById($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_SERIAL_ID], array(
						\haibao\cms\model\filter\FeatureSerial::CONDITION_FIELD_NAME_CURRENT_ISSUE=>$arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_ISSUE]
				));
			}
			
			//如果专题已发布，同步信息到旧专题列表
			if ($model->Status == FeaturePublishModel::STATUS_PUBLISH){
				$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_TITLE] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_NAME];
				$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_DESCRIPTION] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_DESCRIPTION];
				if (!empty($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL])){
					$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_URL] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL];
				}
				$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_TAG] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_CLASS_ID];
				$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_ZT_VIDEO_CATE] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_COLUMN_ID];
				$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_KEYWORDS] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_KEYWORDS];
				$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_LIST_DATE] = $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_LIST_TIME];
				
				if ($trueImgPath){
					$picName = FeatureUtility::saveOldFeatureImage($trueImgPath);
					$oldFeatureSet[ZtSpecialFilter::CONDITION_FIELD_NAME_PIC] = $picName;
				}
				
				$ztSpecialId = $data->getDataMigration($model, $model->Id);
				$ztSpecialData = new ZtSpecialData();
				$ztSpecialData->updateById($ztSpecialId, $oldFeatureSet);
				
				//如果url有改动，发布到新url
				if (!empty($arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL]) && $model->Url != $arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL]){
					$content = FeatureUtility::getFeatureZipByFileName($model->ZipFileName);
					FeatureUtility::extractFeatureZip($model->ZipFileName,$content,$featureName);
					$combinePath = FeatureUtility::combineFeature($model,$ztSpecialId);
					if ($combinePath){
						FeatureUtility::uploadFeature(
							$arrSet[FeaturePublishFilter::CONDITION_FIELD_NAME_URL], 
							$combinePath,
							$model->IsH5
						);
					}
					
					//FeatureUtility::uploadFeatureChannel($model,$ztSpecialId);
				}
			}
			
			//删除旧图片
			if ($imgPath && $pictureId){
				\haibao\cms\business\ImageLibrary::deleteImage($pictureId);
			}
			
			$data->endTranscation();
		}
		catch (\Exception $e){
			$data->rollBack();
			throw $e;
		}
		
		//删除本地图片缓存
		if ($imgPath && $pictureId){
			$imgDir = FeatureUtility::getFeatureTmpPath().'/'.md5($pictureId);
			FileUtility::removeResource($imgDir);
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_EDIT, array_merge($arrSet,array('id'=>$id)));
	}
	
	public static function updateMobileFeature($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_H5_FEATURE_EDIT);
		if (!$arrSet['FeatureName']){
			throw new BusinessException('专题名称不能为空',self::DATA_EMPTY_FEATURE_NAME);
		}
		$data = new FeaturePublishData();
		$data->updateFeature($id,$arrSet);
		self::saveOperateHistory(self::FUNC_CODE_H5_FEATURE_EDIT, array_merge($arrSet,array('id'=>$id)));
	}
	
	public static function addFeatureNew($model){
		$featureIds = array('h5'=>0,'pc'=>0);
		self::checkOperation(self::FUNC_CODE_FEATURE_ADD);
		if (!$model->FeatureName){
			throw new BusinessException('专题名称不能为空',self::DATA_EMPTY_FEATURE_NAME);
		}
		
		$defineFilter = new \haibao\cms\model\filter\FeatureDefine();
		$defineFilter->where(
				\haibao\cms\model\filter\FeatureDefine::CONDITION_FIELD_NAME_FEATURE_NAME,
				Condition::CONDITION_EQUAL,
				$model->FeatureName
		);
		$defineFilter->select(array(
				\haibao\cms\model\filter\FeatureDefine::CONDITION_FIELD_NAME_ID	
		));
		$filter = new FeaturePublishFilter();
		$filter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_ID,	
		));
		$filter->where(
				FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,
				Condition::CONDITION_EQUAL,
				FeaturePublishModel::STATUS_ENABLE		
		);
		$defineFilter->innerJoin(
				$filter,
				null,
				$defineFilter::CONDITION_FIELD_NAME_ID,
				$filter::CONDITION_FIELD_NAME_FEATURE_DEFINE_ID,
				$defineFilter::CONDITION_FIELD_NAME_FEATURE_PUBLISHES,
				true
		);
		
		if (self::searchFeature($defineFilter)){
			throw new BusinessException('专题名称已存在');
		}
		
		foreach ($model->FeaturePublishes as $featurePublish){
			self::checkUrl($featurePublish->Url);
		}
		
		$userId = self::getCurrentUser()->Id;
		$time = new \DateTime();
		foreach ($model->FeaturePublishes as $featurePublish){
			$featurePublish->UpdateBy = $userId;
			$featurePublish->UpdateTime = $time;
			$featurePublish->CreateBy = $userId;
			$featurePublish->CreateTime = $time;
			$featurePublish->Status = FeaturePublishModel::STATUS_NOT_PUBLISH;
		}
		$model->UpdateBy = $userId;
		$model->UpdateTime = $time;
		$model->CreateBy = $userId;
		$model->CreateTime = $time;
		
		//图片,文件
		self::dealPic($model);
		self::dealContent($model);
		
		$data = new FeaturePublishData();
		$data->setReadPerformance(true);
		//$data->beginTranscation();
		
		try{
			$data->addFeature($model);
			
			//如果选择了所属系列及期号，更新serial表
			if ($model->SerialId && $model->Issue){
				$serialData = new \haibao\cms\data\FeatureSerial();
				$serialData->updateById($model->SerialId, array(
						\haibao\cms\model\filter\FeatureSerial::CONDITION_FIELD_NAME_CURRENT_ISSUE=>$model->Issue
				));
			}
			
			foreach ($model->FeaturePublishes as $featurePublish){
				if ($featurePublish->ContentType == self::FEATURE_CONTENT_TYPE_OTHER_FEATURE){
					$otherFeature = self::getFeatureById($featurePublish->OtherFeatureId);
					//同步参数
					$data->copyFeatureParam($model->Id,$otherFeature->FeatureDefineId);
					//同步ini
					$data->copyFeatureIni($featurePublish->Id,$featurePublish->OtherFeatureId);
					//同步动态数据
					$data->copyFeatureDynamic($model->Id,$otherFeature->FeatureDefineId);
				}else{
					$iniModel = new FeatureIniModel();
					$iniModel->WeixinShare = FeatureIniModel::STATUS_DISABLE;
					$iniModel->Vote = FeatureIniModel::STATUS_DISABLE;
					$iniModel->PcShare = FeatureIniModel::STATUS_DISABLE;
					$iniModel->HaibaoHeader = FeatureIniModel::HEADER_DISABLE;
					$iniModel->HaibaoBottom = FeatureIniModel::STATUS_DISABLE;
					$iniModel->GoTop = FeatureIniModel::STATUS_DISABLE;
					$iniModel->Dynamic = FeatureIniModel::STATUS_DISABLE;
					$iniModel->Record = FeatureIniModel::STATUS_DISABLE;
					$iniModel->FeatureId = $featurePublish->Id;
					$data->add($iniModel);
				}
			}
		}
		catch(\Exception $e){
			//$data->rollback();
			throw $e;
		}
		
		//$data->endTranscation();
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_ADD, $model->toArray(true,true));
		
		foreach ($model->FeaturePublishes as $feature){
			if ($feature->IsH5){
				$featureIds['h5'] = $feature->Id;
			}else{
				$featureIds['pc'] = $feature->Id;
			}
		}
		
		return $featureIds;
	}
	
	
	
	/**
	 * 添加专题
	 * @param \haibao\cms\model\data\FeaturePublish $model
	 */
	public static function addFeature($model,$contentType,$otherFeatureId,$svnUrl,$imgPath = null){
		self::checkOperation(self::FUNC_CODE_FEATURE_ADD);
		if (!$model->FeatureName){
			throw new BusinessException('专题名称不能为空',self::DATA_EMPTY_FEATURE_NAME);
		}
		
		$filter = new FeaturePublishFilter();
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_NAME,Condition::CONDITION_EQUAL,$model->FeatureName);
		if (self::searchFeature($filter)){
			throw new BusinessException('专题名称已存在',self::DATA_ERROR_FEATURE_NAME_EXIST);
		}
		
		self::checkUrl($model->Url);
		
		$svnUrl = ltrim(str_replace('\\','/',$svnUrl),'/');
		
		$data = new FeaturePublishData();
		$content = null;
		$fileName = md5($model->FeatureName).microtime(true).'.zip';
		if ($contentType == self::FEATURE_CONTENT_TYPE_UPLOAD){
			$filter = new FeaturePublishFilter();
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_SVN_ADDRESS,Condition::CONDITION_EQUAL,$svnUrl);
			if (self::searchFeature($filter)){
				throw new BusinessException('svn地址已被其他专题使用');
			}
			
			//手动 svn
			$dir = FeatureUtility::getFeatureTmpPath().'/'.date('YmdHis').mt_rand(100,900).'/'.md5($model->FeatureName);
			FileUtility::makeNewDir($dir);
			//if(!SvnUtility::checkout(FeatureUtility::getFullSvnUrl($svnUrl), $dir))
			if (!FeatureUtility::checkoutSvn($svnUrl,$dir))
			{
				throw new BusinessException('从svn获取专题失败');
			}
			$zipFromPath = $dir;
			
			if (!FeatureUtility::checkMainFileNameFromDir($zipFromPath)){
				throw new BusinessException('缺少主文件');
			}
			$zipToPath = $dir.'.zip';
			FileUtility::zipDir($zipFromPath,$zipToPath);
			//$fileName = md5($model->FeatureName).'.zip';
			//$content = file_get_contents($zipToPath);
			FeatureUtility::saveFeatureZipFile($fileName,$zipToPath);
			
			$model->SvnAddress = $svnUrl;
		}
		else if ($contentType == self::FEATURE_CONTENT_TYPE_OTHER_FEATURE){
			//其他专题
			$filter = new FeaturePublishFilter();
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$otherFeatureId);
			$filter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_FILE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_ZIP_FILE_NAME
			));
			$features = self::searchFeature($filter);
			if ($features){
				$feature = array_pop($features);
				/* $file = $data->getFeatureZipById($feature->FileId);
				if ($file){
					$content = $file->getBytes();
					$fileName = $file->file['fileName'];
				} */
				FeatureUtility::saveFeatureZipFile($fileName,'',$feature->ZipFileName);
			}
		}
		
		//图片处理
		if ($imgPath){
			$arr = array(
					'tmp_name'=>$imgPath,
					'name'=>$imgPath
			);
			$imgInfo = \haibao\cms\business\ImageLibrary::saveUpload($arr, \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_FEATURE,false);
			$model->PictureId = $imgInfo['id'];
			$model->PictureName = $imgInfo['imageUrl'];
		}
		
		$userId = self::getCurrentUser()->Id;
		$time = new \DateTime();
		$model->UpdateBy = $userId;
		$model->UpdateTime = $time;
		$model->CreateBy = $userId;
		$model->CreateTime = $time;
		$model->Status = FeaturePublishModel::STATUS_NOT_PUBLISH;
		
		$data = new FeaturePublishData();
		try{
			$data->beginTranscation();
			
			//$model->FileId = $data->storeFeatureZip($content,array('fileName'=>$fileName));
			$model->ZipFileName = $fileName;
			$data->addFeature($model);
			
			//如果选择了所属系列及期号，更新serial表
			if ($model->SerialId && $model->Issue){
				$serialData = new \haibao\cms\data\FeatureSerial();
				$serialData->updateById($model->SerialId, array(
						\haibao\cms\model\filter\FeatureSerial::CONDITION_FIELD_NAME_CURRENT_ISSUE=>$model->Issue
				));
			}
			
			if ($contentType == self::FEATURE_CONTENT_TYPE_OTHER_FEATURE){
				//同步参数
				$data->copyFeatureParam($model->Id,$otherFeatureId);
				//同步ini
				$data->copyFeatureIni($model->Id,$otherFeatureId);
				//同步动态数据
				$data->copyFeatureDynamic($model->Id,$otherFeatureId);
			}
			else{
				$iniModel = new FeatureIniModel();
				$iniModel->WeixinShare = FeatureIniModel::STATUS_DISABLE;
				$iniModel->Vote = FeatureIniModel::STATUS_DISABLE;
				$iniModel->Title = '';
				$iniModel->PcShare = FeatureIniModel::STATUS_DISABLE;
				$iniModel->Keywords = '';
				$iniModel->HaibaoHeader = FeatureIniModel::HEADER_DISABLE;
				$iniModel->HaibaoBottom = FeatureIniModel::STATUS_DISABLE;
				$iniModel->GoTop = FeatureIniModel::STATUS_DISABLE;
				$iniModel->Dynamic = FeatureIniModel::STATUS_DISABLE;
				$iniModel->Description = '';
				$iniModel->Record = FeatureIniModel::STATUS_DISABLE;
				$iniModel->FeatureId = $model->Id;
				$data->add($iniModel);
			}
			
			$data->endTranscation();
		}
		catch(\Exception $e){
			$data->rollback();
			throw $e;
		}
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_ADD, $model->toArray(true,true));
	}
	
	public static function setNewImg($indexPath,$dirPath,$newPath,$oldImgPath,$editName){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
		$newImgContent = '';
		$trueNewPath = FeatureUtility::getFeatureFilePath().$newPath;
		$content = file_get_contents($trueNewPath);
		if (stripos($oldImgPath,'http') === false){
			//本地图片
			$truePath = str_replace(FeatureUtility::FEATURE_BASE_URL,FeatureUtility::FEATURE_PATH,$oldImgPath);
			$truePath = \haibao\frame\Environment::path().$truePath;
			if (file_exists($truePath)){
				file_put_contents($truePath,$content);
			}
		}
		else{
			//网络图片
			//上传图片到本地文件夹，并修改src
			$newImgPath = FeatureUtility::FEATURE_IMG_DIR.'/'.basename($newPath);
			$newImgTruePath = FeatureUtility::getFeatureFilePath().'/'.$dirPath.'/'.$newImgPath;
			FileUtility::filePutContents($newImgTruePath,$content);
			
			//找到edit标签
			$mainFilePath = FeatureUtility::getFeatureFilePath().'/'.$dirPath.'/'.$indexPath;
			$mainFileHtml = file_get_contents($mainFilePath);
			$pattern = '<!--ed|'.$editName.'-->';
			$startPos = mb_stripos($mainFileHtml,$pattern,0,'utf-8');
			$endPos = mb_strripos($mainFileHtml,$pattern,0,'utf-8');
			
			if (false !== $startPos && false !== $endPos){
				//替换img标签的src
				$patternLength = mb_strlen($pattern,'utf-8');
				$editContent = mb_substr($mainFileHtml,$startPos+$patternLength,$endPos-$startPos-$patternLength,'utf-8');
				$imgPattern = '/src(.*?)=(.*?)(\'|")((.*?))(\'|")/is';
				$replace = 'src="'.$newImgPath.'"';
				$newImgContent = preg_replace($imgPattern,$replace,$editContent);
				$startLength = $startPos + mb_strlen($pattern,'utf-8');
				$endLength = mb_strlen($mainFileHtml,'utf-8') - $endPos;
				$newContent = mb_substr($mainFileHtml,0,$startLength,'utf-8').$newImgContent.mb_substr($mainFileHtml,$endPos,$endLength,'utf-8');
				file_put_contents($mainFilePath,$newContent);
			}
		}
		
		return $newImgContent;
	}
	
	public static function clearDir($dir){
		FileUtility::removeResource($dir,false);
	}
	
	/**
	 * 上传
	 */
	public static function uploadFile($file){
		$imgInfo = array('width'=>0,'height'=>0);
		if (FileUtility::getFileType($file['name']) == FileUtility::FILE_TYPE_IMG){
			//if (!FileUtility::checkImgSize($file,$imgInfo)){
			if (fileSize($file['tmp_name']) > 5*1024*1024){
				throw new BusinessException('图片大小不符');
			}
		}
		else if (fileSize($file['tmp_name']) > 200 * 1024 * 1024){
			throw new BusinessException('文件大小不符');
		}
		
		list($imgInfo['width'], $imgInfo['height']) = getimagesize($file['tmp_name']);
		$tmpName = $file['tmp_name'];
		$fileName = $file['name'];
		//判断上传权限？
		$path = '/'.FeatureUtility::FEATURE_TMP_PATH.'/'.date('YmdHis').mt_rand(100,900).'/'.FileUtility::getMd5FileName($fileName);
		$trueFilePath = FeatureUtility::getFeatureFilePath().$path;
		FileUtility::filePutContents($trueFilePath);
		move_uploaded_file($tmpName, $trueFilePath);
		
		return array('path'=>$path,'imgInfo'=>$imgInfo);
	}
	
	/**
	 * 专题搜索
	 * @param \haibao\cms\model\filter\FeaturPublish $filter
	 */
	public static function searchFeature($filter,$isGetOne = false){
		$data = new FeaturePublishData();
		if($isGetOne){
		   return $data->getOne($filter);
		}else{
		   return $data->getAll($filter);
		}
		
	}
	
	public static function checkUrl($url,$id = null){
		if ($url){
			$filter = new FeaturePublishFilter();
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_URL,Condition::CONDITION_EQUAL,$url);
			$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
			if ($id){
				$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$id);
			}
			
			if (self::searchFeature($filter)){
				throw new BusinessException('url已存在',self::DATA_ERROR_FEATURE_URL_EXIST);
			}
			
			if (stripos($url,FeatureUtility::FEATURE_UPLOAD_BASE_URL) === false && stripos($url,FeatureUtility::FEATURE_UPLOAD_BASE_URL_H5) === false){
				throw new BusinessException('url格式不正确');
			}
		}
	}
	
	public static function setFileContent($path,$content,$newPath = null){
		self::checkOperation(self::FUNC_CODE_FEATURE_EDIT);
	
		if ($newPath){
			$trueNewPath = FeatureUtility::getFeatureFilePath().'/'.$newPath;
			$content = file_get_contents($trueNewPath);
		}
	
		$truePath = FeatureUtility::getFeatureFilePath().'/'.$path;
		if (file_exists($truePath) && $content){
			file_put_contents($truePath,$content);
		}
	}
	
	public static function updateFeaturePvCount($featureId,$newEditNum){
		$data = new FeaturePublishData();
		return $data->updateFeaturePvCount($featureId,$newEditNum);
	}
	
	public static function getFeaturePvCountByIds($featureIds){
		$data = new FeaturePublishData();
		return $data->getFeaturePvCountByIds($featureIds);
	}
	
	public static function updateFeatureCommentNum($featureId,$num){
		$data = new FeaturePublishData();
		$data->updateFeatureCommentNum($featureId,$num);
	}
	
	public static function subtractCommentNum($featureId){
		if(!$featureId){
			throw new BusinessException('专题id不能为空',self::DATA_EMPTY_FEATURE_ID);
		}
		$data = new FeaturePublishData();
		$data->subtractCommentNum($featureId);
	}
	public static function getFeatureNum($featurePublishFilter,$isGetOne = false){
	    
	   $FeaturePublishData = new FeaturePublishData();
	   if($isGetOne){
	   		return $FeaturePublishData->getOne($featurePublishFilter);
	   }else{
	   		return $FeaturePublishData->getAll($featurePublishFilter);
	   }
	   
	}
	
	private static function dealContent($model,$isEdit = false){
		foreach ($model->FeaturePublishes as $key=>$featurePublish){
			$hasContent = false;
			
			if ($featurePublish->Id && !$featurePublish->ZipFileName && $isEdit){
				continue;
			}
			
			$str = $featurePublish->ZipFileName;
			$arr = explode('___',$str);
			$type = $arr[0];
			$otherId = $arr[1];
			$svnUrl = ltrim(str_replace('\\','/',$arr[2]),'/');
			$featurePublish->ContentType = $type;
			
			$fileName = md5($model->FeatureName).microtime(true).$featurePublish->IsH5.'.zip';
			if ($type == self::FEATURE_CONTENT_TYPE_UPLOAD && $svnUrl){
				$filter = new FeaturePublishFilter();
				$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
				$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_SVN_ADDRESS,Condition::CONDITION_EQUAL,$svnUrl);
				if (self::searchFeature($filter)){
					throw new BusinessException('svn地址已被其他专题使用');
				}
					
				//手动 svn
				$dir = FeatureUtility::getFeatureTmpPath().'/'.date('YmdHis').mt_rand(100,900).'/'.md5($model->FeatureName);
				FileUtility::makeNewDir($dir);
				//if(!SvnUtility::checkout(FeatureUtility::getFullSvnUrl($svnUrl), $dir))
				if (!FeatureUtility::checkoutSvn($svnUrl,$dir))
				{
					throw new BusinessException('从svn获取专题失败');
				}
	
				$zipFromPath = $dir;
	
				if (!FeatureUtility::checkMainFileNameFromDir($zipFromPath)){
					throw new BusinessException('缺少主文件');
				}
				$zipToPath = $dir.'.zip';
				FileUtility::zipDir($zipFromPath,$zipToPath);
				FeatureUtility::saveFeatureZipFile($fileName,$zipToPath);
	
				$featurePublish->SvnAddress = $svnUrl;
				$hasContent = true;
			} else if ($type == self::FEATURE_CONTENT_TYPE_OTHER_FEATURE && $otherId){
				$featurePublish->OtherFeatureId = $otherId;
				//其他专题
				$filter = new FeaturePublishFilter();
				$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$otherId);
				$filter->select(array(
						FeaturePublishFilter::CONDITION_FIELD_NAME_FILE_ID,
						FeaturePublishFilter::CONDITION_FIELD_NAME_ZIP_FILE_NAME
				));
				$features = self::searchFeature($filter);
				if ($features){
					$feature = array_pop($features);
					FeatureUtility::saveFeatureZipFile($fileName,'',$feature->ZipFileName);
				}
	
				$hasContent = true;
			}
			
			$featurePublish->ZipFileName = $fileName;
			
			if (!$hasContent && !$featurePublish->Id){
				unset($model->FeaturePublishes[$key]);
			}
		}
	
		if (!$model->FeaturePublishes && !$isEdit){
			throw new \Exception('请至少完善一个站点的专题内容');
		}
	}
	
	private static function dealPic($model){
		foreach ($model->FeaturePublishes as $featurePublish){
			if ($featurePublish->PictureId){
				$imgPath = $featurePublish->PictureId;
				$arr = array(
						'tmp_name'=>$imgPath,
						'name'=>$imgPath
				);
				$imgInfo = \haibao\cms\business\ImageLibrary::saveUpload($arr, \haibao\cms\model\data\ImageLibrary::UPLOAD_SOURCE_FEATURE,false);
				$featurePublish->PictureName = $imgInfo['imageUrl'];
				$featurePublish->PictureId = $imgInfo['id'];
			}
		}
	}
}	